int main()
{
;
}


/*
....................................................... TCP C SERVER ....................................................................................

#include<netinet/in.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int i, receiver ,listensock , size;
    char recvBuffer[100] , sendbuffer[100];
    
    struct sockaddr_in thisSocket , clientSock ;
    
    listensock = socket(PF_INET , SOCK_STREAM , 0);
    
    thisSocket.sin_family = AF_INET;
    thisSocket.sin_addr.s_addr = inet_addr("127.0.0.1");
    thisSocket.sin_port = htons(7778);
    
    bind(listensock , (struct sockaddr *)&thisSocket , sizeof(thisSocket));
    listen(listensock , 1);
    size = sizeof(struct sockaddr_in);
    receiver = accept(listensock , (struct sockaddr *)&clientSock , &size);

    while(1)
        {
            //////////// RECV PART ////////////
	    i = recv(receiver,recvBuffer,100,0);
            if(i > 0)
                {
                    recvBuffer[i] = '\0';
                    printf("%s",recvBuffer);
                }
            //////////// SEND PART ////////////
	    fgets(sendbuffer,100,stdin);
	    send(receiver , sendbuffer , strlen(sendbuffer),0);
        }
}

....................................................... TCP C CLIENT ....................................................................................

#include<netinet/in.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int i, receiver;
    char recvBuffer[100] , sendbuffer[100];
    
    struct sockaddr_in thisSocket;
    
    receiver = socket(PF_INET , SOCK_STREAM , 0);
    
    thisSocket.sin_family = AF_INET;
    thisSocket.sin_addr.s_addr = inet_addr("127.0.0.1");    
    thisSocket.sin_port = htons(7778);

    connect(receiver,(struct sockaddr *)&thisSocket,sizeof(thisSocket));

    while(1)
        {
            //////////// SEND PART ////////////
	    fgets(sendbuffer,100,stdin);
            send(receiver,sendbuffer,strlen(sendbuffer),0);
            //////////// RECV PART ////////////
	    i =recv(receiver,recvBuffer,100,0);
	    if(i>0)
                {
                    recvBuffer[i]='\0';
                    printf("%s",recvBuffer);
                }
        }
}

........................................................ TCP JAVA SERVER ...................................................................................

import java.io.*;
import java.net.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        ServerSocket ss = null;
        Socket s = null;

        ss = new ServerSocket(7777);
        s = ss.accept();

        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        DataInputStream dis = new DataInputStream(s.getInputStream());

        while(true)
        {
            String from_Client = dis.readUTF();
            System.out.println(from_Client);

            Scanner sc = new Scanner(System.in);
            String to_client = sc.nextLine();
            dos.writeUTF(to_client);
        }


    }
}



........................................................ TCP JAVA CLIENT ...................................................................................

import java.io.*;
import java.net.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        Socket s = null;
        s = new Socket("127.0.0.1",7777);
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        DataInputStream dis = new DataInputStream(s.getInputStream());
        while (true)
        {
            Scanner sc = new Scanner(System.in);
            String to_server = sc.nextLine();
            dos.writeUTF(to_server);
            String from_server = dis.readUTF();
            System.out.println(from_server);
        }

    }
}


........................................................ UDP C CLIENT ...................................................................................

#include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>

int main()
{
	int sd,portno,i;
	struct sockaddr_in cli;
	char msg[80],ip[80];
	sd=socket(AF_INET,SOCK_DGRAM,0);
	bzero(&cli,sizeof(cli));
	printf("enter the port no");
	scanf("%d",&portno);
	cli.sin_port=htons(portno);
	cli.sin_family=AF_INET;
	printf("enter the ip address");
	scanf("%s",ip);
	cli.sin_addr.s_addr=inet_addr(ip);
	printf("\nenter the message to send\n");
	scanf("%s",msg);
	sendto(sd,msg,80,0,(struct sockaddr*)&cli,sizeof(cli));
	printf("\nreply from server\n");
	i=sizeof(cli);
	recvfrom(sd,msg,80,0,(struct sockaddr*)&cli,&i);
	 printf("%s",msg);

	return 0;
        //scanf("%s",msg);
}


........................................................ UDP C SERVER ...................................................................................
#include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>

int  main()
{
        int sd,portno,i;
        struct sockaddr_in cli;
        char msg[80],ip[80];
        sd=socket(AF_INET,SOCK_DGRAM,0);
        bzero(&cli,sizeof(cli));
        printf("enter the port no");
        scanf("%d",&portno);
        cli.sin_port=htons(portno);
        cli.sin_family=AF_INET;
        cli.sin_addr.s_addr=INADDR_ANY;
	 bind(sd,(struct sockaddr*)&cli,sizeof(cli));

	
        printf("\nreply from client\n");
        i=sizeof(cli);
        recvfrom(sd,msg,80,0,(struct sockaddr*)&cli,&i);
	printf("%s",msg);
	printf("\nenter the message to send\n");
	scanf("%s",msg);

        sendto(sd,msg,80,0,(struct sockaddr*)&cli,sizeof(cli));


	return 0;
}


........................................................ UDP JAVA SERVER ...................................................................................

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])throws IOException {


        DatagramSocket ds = new DatagramSocket(6058);
        byte receiver[] = new byte[1024];
        byte sender[] = new byte[1024];


        DatagramPacket RECVdp = new DatagramPacket(receiver, receiver.length);
        ds.receive(RECVdp);

        String sentence = new String(RECVdp.getData());
        System.out.println(sentence);

        InetAddress ip = RECVdp.getAddress();
        int port = RECVdp.getPort();

        Scanner sc = new Scanner(System.in);
        String reply = sc.nextLine();
        sender = reply.getBytes();

        DatagramPacket SENDdp = new DatagramPacket(sender, sender.length, ip, port);

        ds.send(SENDdp);
        ds.close();
    }
}


........................................................ UDP JAVA CLIENT ...................................................................................

import java.io.*;
import java.net.*;
import java.util.*;

public class Main {

    public static void main(String[] args)throws IOException {
        byte receiver[] = new byte[1024];
        byte sender[] = new byte[1024];

        DatagramSocket ds = new DatagramSocket();
        InetAddress ip = InetAddress.getByName("localhost");

        Scanner sc = new Scanner(System.in);
        String reply = sc.nextLine();
        sender = reply.getBytes();

        DatagramPacket SENDdp = new DatagramPacket(sender,sender.length,ip,6058);
        ds.send(SENDdp);

        DatagramPacket RECVdp = new DatagramPacket(receiver,receiver.length);
        ds.receive(RECVdp);
        String from_server = new String(RECVdp.getData());
        System.out.println(from_server);

        ds.close();

    }
}



........................................................ FILE IN C ...................................................................................

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
  FILE *FP;
  int i=0;
  FP = fopen("test.txt","r");
  char str[1024];
  char c;
  while(!feof(FP))
  {
    str[i]=fgetc(FP);;
    i++;
  }
  str[i-1]='\0';
  puts(str);
}


........................................................ FILE IN JAVA ...................................................................................

import java.io.*;

public class Main {
    public static void main(String args[]) throws IOException
    {
        int i=0;
        FileInputStream in = null;
        char str[] = new char[1000];

	in = new FileInputStream("test.txt");

	int c;
	while ((c = in.read()) != -1) {
	    str[i++]=(char)c;
	}
	System.out.println(str);

        }
    }
    
*/

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

#include<stdio.h>
int main()
{
  printf("OSSSSSS");
}
/*        --------------------------------- reader writer--------------------------------
 #include<stdio.h>
#include<pthread.h>
#include<string.h>
#include<semaphore.h>

void *read(), *write();
int a;
int hr=0, min=0, sec=0;

void *ret;
pthread_t rth, wth;
pthread_mutex_t m;
void main()
{
	int r1=1, r2=1;
	a=10;

	//create thread	
	r1=pthread_create(&rth,NULL,&read,NULL);
	r2=pthread_create(&wth,NULL,&write,NULL);	
	
	//check for success/failure
	if(r1==0)
		printf("rth created successfully.\n");
	else
		printf("rth thread creation failed.\n");
	if(r2==0)
		printf("wth created successfully.\n");
	else
		printf("wth thread creation failed.\n");

	//initializing mutex
	pthread_mutex_init(&m,NULL);

	//Join threads
	pthread_join(rth,ret);
	pthread_join(wth,ret);
}

void *read()
{
	while(1)
	{

		pthread_mutex_lock(&m);
		//printf("%d\n", a);
		printf("%d:%d:%d\n", hr, min, sec);
		pthread_mutex_unlock(&m);
		sleep(1);
	}
	pthread_exit(&ret);
}

void *write()
{
	while(1)
	{
		pthread_mutex_lock(&m);
		sec++;
		if(sec==60)
		{
			sec=0; 
			min++;	
		}
		if(min==60)
		{
			min=0;
			hr++;
		}
		if(hr==24)
		{
			hr=0;
		}
		pthread_mutex_unlock(&m);
		sleep(1);
	}
	pthread_exit(&ret);
}
//-----------------------------------------------------------------------------------------------------------------
 */






/*
....................................................... producer-consumer-semaphore ....................................................................................

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>

int buffer[10],tid1,tid2,cnt = 0;
void *producer();
void *consumer();
void *ret;
sem_t s1,s2;
pthread_t pro,con;

void main()
{
	sem_init(&s1,0,0);
	sem_init(&s2,0,1);
	tid1 = pthread_create(&pro,NULL,&producer,NULL);	
	if(tid1 == 0)
		printf("\nproducer created successfully");
	tid2 = pthread_create(&con,NULL,&consumer,NULL);	
	if(tid2 == 0)
		printf("\nconsumer created successfully");	

	pthread_join(pro,NULL);	
	pthread_join(con,NULL);	
	sem_destroy(&s1);
	sem_destroy(&s2);
}

void *producer()
{
	int item_produced = 0;
	printf("\nproducing..");
	while(cnt < 10)
	{
		sem_wait(&s2);
		item_produced++;
		buffer[cnt] = item_produced;
		printf("\nitem produced : %d",item_produced);	
		cnt++;
		sem_post(&s1);
		sleep(1);
	}	
	return(&ret);
}


void *consumer()
{
	int item_consumed;
	printf("\nconsuming..");
	while(cnt >= 0)
	{
		sem_wait(&s1);
		cnt--;
		item_consumed = buffer[cnt];
		printf("\nitem consumed : %d",item_consumed);	
		sem_post(&s2);
		sleep(1);
	}
	return(&ret);
}



....................................................... job scheduling ....................................................................................

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

void FCFS(int num_proc, int arrival_time[], int burst_time[])
{
    int time_cntr = 0,next = 0,j,i,finish_time[num_proc],turnaround_time[num_proc],waiting_time[num_proc],done[num_proc+1];

    for(i=0; i<=num_proc; ++i)
    {
        done[i] = 0;
    }
    for(i=0; i<num_proc; ++i)
    {
        next = num_proc;
        for(j=0; j<=num_proc; ++j)
        {
            if(arrival_time[j] < arrival_time[next] && done[j] == 0)
            {
                //cout<<"next changed"<<endl<<endl;
                next = j;
            }
        }
        cout<<"next : "<<next<<endl;
        done[next] = 1;
        time_cntr = time_cntr + burst_time[next];
        finish_time[next] = time_cntr;
        cout<<"FT : "<<finish_time[next]<<endl;
        turnaround_time[next] = finish_time[next] - arrival_time[next];
        cout<<"TT : "<<turnaround_time[next]<<endl;
        waiting_time[next] = turnaround_time[next] - burst_time[next];
        cout<<"WT : "<<waiting_time[next]<<endl<<endl;
    }
    return;
}

void FCFS_new(int num_proc, int arrival_time[], int burst_time[])
{
    int time_cntr=0,i,j,k,min_proc = 100,next,ft,wt,tt,done[num_proc];
    queue< int > ready_q;

    for(i=0; i<=num_proc; ++i)
    {
        done[i] = 0;
    }

    for(i=0; i<num_proc; ++i)
    {
        min_proc = 100;
        for(j=0; j<num_proc; ++j)
        {
            if(arrival_time[j] < min_proc && done[j] == 0)
            {
                min_proc = arrival_time[j];
                next = j;
            }
        }
        ready_q.push(next);
        done[next] = 1;
    }
    for(k=0; k<num_proc; ++k)
    {
        cout<<endl<<endl;
        next = ready_q.front();
        ready_q.pop();
        cout<<"next proc : "<<next;
        time_cntr = time_cntr + burst_time[next];
        ft  = time_cntr;
        cout<<"finish time : "<<ft<<endl;
        tt = ft - arrival_time[next];
        cout<<"turnaround time : "<<tt<<endl;
        wt = tt - burst_time[next];
        cout<<"waiting time : "<<wt<<endl;
    }
    return;
}

void SJF_nonP(int num_proc, int arrival_time[], int burst_time[])
{
    int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;

    for(i=0; i<num_proc; ++i)
    {
        done[i] = 0;
        if(arrival_time[i] == 0)
        {
            done[i] = 1;
            next = i;
        }
    }

    time_cntr = burst_time[next];
    ft = time_cntr;
    cout<<"Finish time : "<<ft<<endl;
    tt = ft - arrival_time[next];
    cout<<"Turnaround time : "<<tt<<endl;
    wt = tt - burst_time[next];
    cout<<"Waiting time : "<<wt<<endl;

    for(i=1; i<num_proc; ++i)
    {
        min_proc = 100;
        for(j=0; j<num_proc; ++j)
        {
            if(arrival_time[j] < time_cntr)
            {
                if(burst_time[j] < min_proc && done[j] == 0)
                {
                    min_proc = burst_time[j];
                    next = j;
                }
            }
        }
        time_cntr = time_cntr + burst_time[next];
        ft = time_cntr;
        cout<<endl<<"Finish time : "<<ft<<endl;
        cout<<"Turnaround time : "<<tt<<endl;
        cout<<"Waiting time : "<<wt<<endl;
        
    }

    return;
}
void print_job(int ft,int tt,int wt)
{
    cout<<endl<<"Finish time : "<<ft<<endl;
    cout<<"Turnaround time : "<<tt<<endl;
    cout<<"Waiting time : "<<wt<<endl;
}   
void SJF_P(int num_proc, int arrival_time[], int burst_time[])
{
    int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
    int bt1[100];
    for(i=0; i<num_proc; ++i)
    {
        done[i] = 0;
        bt1[i] = burst_time[i];
        // if(arrival_time[i] == 0)
        // {
        // 	if(burst_time[i] == 1)
        //     	done[i] = 1;
        //     next = i;
        // }
    }
    int counter = 0;
    	
    while(1)
    {
    	min_proc = 100;
    	for(i = 0;i < num_proc;i++)
    	{
    		if(burst_time[i] < min_proc && done[i] == 0 && arrival_time[i] <= counter)
    		{

    			min_proc = burst_time[i];
    			next = i;
    		}
    	}
    	counter++;
    	time_cntr++;
    	cout<<"selected "<<min_proc<<endl;
		if(min_proc == 100)
			break;
		
		if(burst_time[next] == 1)
		{
			done[next] = 1;
			//next = i;
			//time_cntr = time_cntr + bt1[next];
			ft = time_cntr;
			tt = ft - arrival_time[next];
			wt = tt - bt1[next];
			print_job(ft,tt,wt);
			burst_time[next]--;
		}
		else
			burst_time[next]--;
		// for(int i = 0;i < num_proc;i++)
		// 	cout<<burst_time[i]<<"\t"<<done[i]<<endl;	
    }

    return;
}

void Priority_nonP(int num_proc, int arrival_time[], int burst_time[], int priority[])
{
    int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;

    for(i=0; i<num_proc; ++i)
    {
        done[i] = 0;
        if(arrival_time[i] == 0)
        {
            done[i] = 1;
            next = i;
        }
    }

    time_cntr = time_cntr + burst_time[next];
    ft = time_cntr;
    cout<<"Finish time : "<<ft<<endl;
    tt = ft - arrival_time[next];
    cout<<"Turnaround time : "<<tt<<endl;
    wt = tt - burst_time[next];
    cout<<"Waiting time : "<<wt<<endl;

    for(i=1; i<num_proc; ++i)
    {
        int max_priority = 100;
        for(j=0; j<num_proc; ++j)
        {
            if(arrival_time[j] < time_cntr)
            {
                if(priority[j] < max_priority && done[j] == 0)
                {
                    max_priority = priority[j];
                    next = j;
                }
            }
        }
        time_cntr = time_cntr + burst_time[next];
        ft = time_cntr;
        cout<<"Finish time : "<<ft<<endl;
        tt = ft - arrival_time[next];
        cout<<"Turnaround time : "<<tt<<endl;
        wt = tt - burst_time[next];
        cout<<"Waiting time : "<<wt<<endl;
        done[next] = 1;
    }

    return;
}

void Priority_P(int num_proc, int arrival_time[], int burst_time[], int priority[])
{
	int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
	int bt1[num_proc];
    for(i=0; i<num_proc; ++i)
    {
        done[i] = 0;
        bt1[i] = burst_time[i];
        // if(arrival_time[i] == 0)
        // {
        //     done[i] = 1;
        //     next = i;
        // }
    }

    // time_cntr = time_cntr + burst_time[next];
    // ft = time_cntr;
    // cout<<"Finish time : "<<ft<<endl;
    // tt = ft - arrival_time[next];
    // cout<<"Turnaround time : "<<tt<<endl;
    // wt = tt - burst_time[next];
    // cout<<"Waiting time : "<<wt<<endl;

    while(1)
    {
        int max_priority = 100;
        for(j=0; j<num_proc; ++j)
        {
            if(arrival_time[j] <= time_cntr)
            {
                if(priority[j] < max_priority && done[j] == 0)
                {
                    max_priority = priority[j];
                    next = j;
                }
            }
        }
        time_cntr++;
    	//cout<<"selected "<<max_priority<<endl;
		if(max_priority == 100)
			break;
		
		if(burst_time[next] == 1)
		{
			done[next] = 1;
			//next = i;
			//time_cntr = time_cntr + bt1[next];
			ft = time_cntr;
			tt = ft - arrival_time[next];
			wt = tt - bt1[next];
			print_job(ft,tt,wt);
			burst_time[next]--;
		}
		else
			burst_time[next]--;
        // ft = time_cntr;
        // tt = ft - arrival_time[next];
        // wt = tt - burst_time[next];
        //done[next] = 1;
    }
    return;
}

void RR(int num_proc, int arrival_time[], int burst_time[], int priority[])
{
    return;
}

int main()
{
    int i,j,k,num_proc,choice;

    while(1)
    {
        cout<<"Enter your choice :"<<endl;
        cout<<"1] FCFS"<<endl<<"2] SJF(Non-Preemptive)"<<endl<<"3] SJF(Preemptive)"<<endl<<"4] Priority(Non-Preemptive)"<<endl<<"5] Priority(Preemptive)"<<endl<<"6] Round-Robin"<<endl<<"7] EXIT"<<endl<<endl;
        cin>>choice;

        if(choice == 7)
            break;

        cout<<"Enter number of processes :"<<endl<<endl;
        cin>>num_proc;

        int arrival_time[num_proc+1],burst_time[num_proc],priority[num_proc];

        cout<<"Enter Arrival Times, Burst Times and Priority of processes :"<<endl<<endl;
        for(i=0; i<num_proc; i++)
        {
            cin>>arrival_time[i];
            cin>>burst_time[i];
            cin>>priority[i];
        }
        arrival_time[num_proc] = 10000;

        switch(choice)
        {
        case 1:
            FCFS_new(num_proc, arrival_time, burst_time);
            break;

        case 2:
            SJF_nonP(num_proc, arrival_time, burst_time);
            break;

        case 3:
            SJF_P(num_proc, arrival_time, burst_time);
            break;

        case 4:
            Priority_nonP(num_proc, arrival_time, burst_time, priority);
            break;

        case 5:
            Priority_P(num_proc, arrival_time, burst_time, priority);
            break;

        case 6:
            RR(num_proc, arrival_time, burst_time, priority);
            break;
        }
    }
    return 0;
}


........................................................ page replacement FIFO ONLY ...................................................................................

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
using namespace std;

void FIFO(int num_of_frames,int num_of_pages,int pages[],int time[],int cnt)
{
    int i,j,k,m,n,memory[num_of_frames],num_of_pf = 0,no_pf = 0, valid_pf = 0;
    queue< int > mem;

    for(j=0; j<num_of_frames; ++j)
        memory[j] = 0;

    for(j=0; j<cnt; ++j)
    {
        for(k=0; k<num_of_frames; ++k)
        {
            if(memory[k] == pages[j])
            {
                no_pf = 1;
                break;
            }
            else
                continue;
        }
        if(no_pf == 1)
        {
            cout<<"j : "<<j<<" no pf"<<endl;
            no_pf = 0;
            continue;
        }
        else
        {
            num_of_pf++;
            cout<<"j : "<<j<<" pf : "<<num_of_pf<<endl;

            for(m=0; m<num_of_frames; ++m)
            {
                if(memory[m] == 0)
                {
                    valid_pf = 1;
                    break;
                }
            }
            if(valid_pf == 1)
            {
                memory[m] = pages[j];
                mem.push(pages[j]);
                valid_pf = 0;
                continue;
            }
            else
            {
                int replace_page = mem.front();
                mem.pop();
                mem.push(pages[j]);
                for(n=0; n<num_of_frames; ++n)
                {
                    if(memory[n] == replace_page)
                    {
                        memory[n] = pages[j];
                        break;
                    }
                }
            }
        }
    }
    cout<<"number of page faults : "<<num_of_pf<<endl;
}

void Optimal(int pages[],int time[],int num_of_frames,int num_of_pages)
{

}

int main()
{
    int num_of_frames,num_of_pages,i=0,j,k,cnt=0;
    cout<<"Enter no. of frames : ";
    cin>>num_of_frames;
    cout<<"Enter no. of pages : ";
    cin>>num_of_pages;
    int pages[num_of_pages],time[num_of_pages],choice;

    cout<<"Enter the Reference String : ";
    while(1)
    {
        cin>>pages[cnt];
        if(pages[cnt] == 0)
            break;
        cnt++;
    }

    cout<<"Enter your choice : "<<endl;
    cout<<"1] FIFO"<<endl<<"2] Optimal"<<endl<<"3] LRU"<<endl;
    cin>>choice;

    switch(choice)
    {
    case 1:
        FIFO(num_of_frames,num_of_pages,pages,time,cnt);
        break;

    case 2:
        Optimal(pages,time,num_of_frames,num_of_pages);
        break;

    case 3:
        //LRU(pages,time,num_of_frames,num_of_pages);
        break;

    case 4:
        exit(0);
    }

    return 0;
}

........................................................ DISC SCHEDULING ...................................................................................

#include<stdio.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;

void fcfs(int num, int req[], int current)
{
    int i,seek=0;
    for(i=0; i<num; ++i)
    {
        if(current <= req[i])
            seek = seek + (req[i] - current);

        if(current > req[i])
            seek = seek + (current - req[i]);

        current = req[i];
    }
    cout<<"total seek time : "<<seek<<endl;
}

int find_min(int num, int current, int req[], int done[])
{
    int temp,i,j,arr[num];
    for(i=0; i<num; ++i)
    {
        temp = current - req[i];
        if(temp < 0)
            temp = temp - (2 * temp);
        arr[i] = temp;
    }
    int min_req = 10000,min_index;
    for(i=0; i<num; ++i)
    {
        if(min_req > arr[i] && done[i] == 0)
        {
            min_req = arr[i];
            min_index = i;
        }
    }
    return min_index;
}

void sstf(int num, int req[], int current)
{
    int i,j,done[num],next,seek = 0;
    for(i=0; i<num; ++i)
        done[i] = 0;

    for(i=0; i<num; ++i)
    {
        next = find_min(num,current,req,done);
        if(current > req[next])
            seek = seek + (current - req[next]);

        else if(current <= req[next])
            seek = seek + (req[next] - current);

        current = req[next];
        done[next] = 1;
    }
    cout<<seek;
}

void scan(int num, int req[], int current)
{
    int i,j,newreq[num+1],cur_index;
    for(i=0; i<num; ++i)
    {
        newreq[i] = req[i];
    }
    newreq[num] = current;
    sort(newreq,(newreq+num+1));
    for(i=0; i<(num+1); ++i)
    {
        if(newreq[i] == current)
        {
            cur_index = i;
            break;
        }
    }
    cout<<"current at : "<<cur_index<<endl;
    int seek = 0,next = cur_index;
    for(i=0; i<(num-cur_index); ++i)
    {
        seek = seek + (newreq[next+1] - newreq[next]);
        next++;
    }
    next = cur_index-1;
    seek = seek + (newreq[num] - newreq[next]);
    for(i=1; i<cur_index; ++i)
    {
        seek = seek + (newreq[next] - newreq[next-1]);
        next--;
    }
    cout<<"final : "<<seek<<endl;
}

int main()
{
    int i,num,cur;
    cout<<"enter current head position : ";
    cin>>cur;
    cout<<"enter number of requests : ";
    cin>>num;
    int req[num];
    cout<<"enter requests : ";
    for(i=0; i<num; ++i)
        cin>>req[i];
    //fcfs(num,req,cur);
    //sstf(num,req,cur);
    scan(num,req,cur);
    return 0;
}


........................................................ BUBBLE SORT ...................................................................................
#/bin/sh

echo "Number of elements : "
read num

echo "Enter the elements : "
for((i=0; i<num; ++i))
do
	read arr[$i]	
done

for((i=0; i<num; ++i))
do
	for((j=0; j<num-1; ++j))
	do
		if [ ${arr[$j]} -gt ${arr[$j+1]} ]
		then
			temp=${arr[$j]}
			arr[$j]=${arr[$j+1]}
			arr[$j+1]=$temp			
		fi	
	done
done

echo "Sorted elements are : "
for((i=0; i<num; ++i))
do
	echo ${arr[$i]}
done	


........................................................ LRU ...................................................................................

#include <stdio.h>
#define MAX 20
typedef struct page
{
    int data;
    int hit;
} page;
void main()
{
    int i,j,k,flag,cnt=0,q=0,flag2=0,temp,max,mix=0,f3=0;
    page p[MAX];
    int frame[MAX];
    int fs,nr;
    printf("\nEnter number of references: ");
    scanf("%d",&nr);
    printf("\nEnter %d references: ",nr);
    for(i=0; i<nr; i++)
    {
        scanf("%d",&frame[i]);
    }

    printf("\nEnter frame size: ");
    scanf("%d",&fs);

    for(i=0; i<fs; i++)
    {
        p[i].data=-1;
        p[i].hit=0;
    }

    j=0;
    for(i=0; i<nr; i++)
    {
        for(k=0; k<fs; k++)
        {
            if(p[k].data!=-1)
                p[k].hit++;
        }

        flag=0;

        printf("\nREF: %d",frame[i]);
        for(k=0; k<fs; k++)
        {
            if(p[k].data==frame[i])
            {
                p[k].hit=1;
                flag=1;
                temp=k;
                break;
            }
        }

        if(flag!=1 && f3!=1)
        {
            p[j].data=frame[i];
            p[j].hit=1;
            temp=j;
            cnt++;
            j++;
        }

        if(f3==1 && flag!=1)
        {
            max=0;
            mix=0;
            for(k=0; k<fs; k++)
            {
                if(max < p[k].hit)
                {
                    max=p[k].hit;
                    mix=k;
                }
            }

            flag2=0;
            for(k=0; k<fs; k++)
            {
                if(flag2!=1)
                {
                    j=mix;
                    p[j].data=frame[i];
                    p[j].hit=1;
                    temp=j;
                    cnt++;
                    flag2=1;
                }
            }
        }
        if(j==fs)
        {
            j=0;
            f3=1;
        }

        for(k=0; k<fs; k++)
        {
            if(temp==k)
            {
                printf("\n->%d-%d",p[k].data,p[k].hit);
            }
            else
            {
                printf("\n%d-%d",p[k].data,p[k].hit);
            }
        }
        printf("\n-------\n");
    }
    printf("\nPage Fault: %d\n",cnt);
}




........................................................ IDEAL process SCHEDULING PROGRAM ...................................................................................

#include<stdio.h>
//Definition of structure for process
struct process
{
	char name[20];
	int flag,arrival,burst,initburst,prio;
};
//Queue Definition along with queue functions
struct queue
{
	struct process data[20];
	int r,f;
};
void init(struct queue *q)
{
	q->r=q->f=-1;
}
int empty(struct queue q)
{
	if(q.r==-1)
		return(1);
	return(0);
}
//Normal enqueue(used in FCFS and Round Robin)
void enqueue(struct queue *q,struct process d)
{
	if(empty(*q))
		q->r=q->f=0;
	else q->r=q->r+1;
	q->data[q->r]=d;
}
/*Enqueue for sjf scheduling. In this, we maintain a priority based queue
where priority is given to the shortest job, i.e, the job with min. burst time is
always at the front of the queue*/
void sjfenqueue(struct queue *q, struct process d)
{
	int x,y;
	if(empty(*q))
	{
		q->r=q->f=0;
		q->data[q->r]=d;
	}
	else
	{
		x=q->f;
		y=q->r;
		//Go through the queue to find position of new process d
		while(q->data[x].burst<=d.burst&&x<=y)
			x++;
		//Shift elements one place to the right to make space for d
		while(y>=x)
		{
			q->data[y+1]=q->data[y];
			y--;
		}
		q->data[x]=d;
		q->r=q->r+1;
	}
}
/*Enqueue for priority scheduling. Same as above except the priority is now
using process priority instead of burst time*/
void prioenqueue(struct queue *q, struct process d)
{
        int x,y;
        if(empty(*q))
        {
                q->r=q->f=0;
                q->data[q->r]=d;
        }
        else
        {
                x=q->f;
                y=q->r;
                //Go through the queue to find position of new process d
                while(q->data[x].prio<=d.prio&&x<=y)
                        x++;
                //Shift elements one place to the right to make space for d
                while(y>=x)
                {
                        q->data[y+1]=q->data[y];
                        y--;
					}
					q->data[x]=d;
					q->r=q->r+1;
		}
}
struct process dequeue(struct queue *q)
{
	struct process d;
	d=q->data[q->f];
	if(q->f==q->r)
		q->f=q->r=-1;
	else q->f=q->f+1;
	return(d);
}
void accept(struct process p[20],int n);
void fcfs(struct process p[20],int n);
void sjf(struct process p[20],int n);
void prio(struct process p[20],int n);
void sjfpre(struct process p[20],int n);
void priopre(struct process p[20],int n);
void rr(struct process p[20],int n);
void main()
{
	int ch,n,i;
	struct process p[20];
	printf("\nEnter number of processes: ");
	scanf("%d",&n);	
	accept(p,n);
	do
	{
	for(i=0;i<n;i++)
		p[i].flag=0;
	printf("\nMENU:\n1.FCFS\n2.SJF\n3.SJF-preemptive\n4.Priority\n5.Priority-preemptive\n6.Round Robin\n7.Re-enter processes\n8.Exit\n\n\tEnter your choice: ");
	scanf("%d",&ch);
	switch(ch)
	{
		case 1: fcfs(p,n);
			break;
		case 2: sjf(p,n);
			break;
		case 3: sjfpre(p,n);
			break;
		case 4: prio(p,n);
			break;
		case 5: priopre(p,n);
			break;
		case 6: rr(p,n);
			break;		
		case 7: printf("\nEnter number of processes: ");
			scanf("%d",&n);
			accept(p,n);
			break;
		case 8: break;		
		default:printf("\nWrong choice entered\n");
			break;
	}
	}while(ch!=8);
}
void accept(struct process p[20],int n)
{
	int i;
	char s[2];	
	for(i=0;i<n;i++)
	{
	printf("\nProcess %d: ",i+1);
//We use sprintf to give thee processes names like 'P1','P2',etc.
	sprintf(p[i].name,"P%d",i+1);
	printf("\nEnter arrival time: ");
	scanf("%d",&p[i].arrival);
	printf("\nEnter burst time: ");
	scanf("%d",&p[i].initburst);
	//initburst is the initial burst time, and 'burst' is current burst time
	p[i].burst=p[i].initburst;
	}
}
void fcfs(struct process p[20],int n)
{
	int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
	float avgta=0,avgwt=0;	
	struct queue q;
	init(&q);
	struct process pc;
	/* pc is the current process. Since initially, there is no process in
	pc, we set its flag to -1*/
	pc.flag=-1;
	printf("\nGANTT CHART\n\n\t|");
	/*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses*/
	for(i=0;comp<n;i++)
	{
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
		for(j=0;j<n;j++)
		{
			if(p[j].arrival<=i && p[j].flag==0)
			{
				p[j].flag=1;
				enqueue(&q,p[j]);
			}
		}
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
		if(pc.flag==-1)
		{
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
			if(empty(q) && flag1==0)
				flag1=1;
			else if(empty(q) && flag1==1)
				continue;
			else if(!empty(q) && flag1==1)
			{
				printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */				
				pretime[cnt++]=i;
				flag1=0;
				pc=dequeue(&q);
			}
			else pc=dequeue(&q);
		}
/*Decrement burst time to indicate completion of one burst */
		pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
		if(pc.burst==0)
		{
			turnaround=(i+1)-pc.arrival;
			waiting=turnaround-pc.initburst;
			avgta+=turnaround;
			avgwt+=waiting;
			pretime[cnt++]=i+1;
			comp++;
			printf("%s|",pc.name);
			pc.flag=-1;
		}
	}
//Display the times below the Gantt Chart
	printf("\n\t0 ");
	for(i=0;i<cnt;i++)
	{	
		if(pretime[i]>9)
			printf("%d ",pretime[i]);
		else	printf (" %d ",pretime[i]);
	}
//Calculate avg. TA and WT
	avgta=avgta/n;
	avgwt=avgwt/n;
	printf("\n\nAvg. TA time: %.2f",avgta);
	printf("\n\nAvg. WT time: %.2f",avgwt);
}

//Exactly the same as FCFS, except we use sjfenqueue function instead of enqueue function
void sjf(struct process p[20],int n)
{
	int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
	float avgta=0,avgwt=0;	
	struct queue q;
	init(&q);
	struct process pc;
	/* pc is the current process. Since initially, there is no process in
	pc, we set its flag to -1*/
	pc.flag=-1;
	printf("\nGANTT CHART\n\n\t|");
	/*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when 	i=1. The loop will run until completed processes(comp) equals no. of proceses
	*/
	for(i=0;comp<n;i++)
	{
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
		for(j=0;j<n;j++)
		{
			if(p[j].arrival<=i && p[j].flag==0)
			{
				p[j].flag=1;
				sjfenqueue(&q,p[j]);
			}
		}
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
		if(pc.flag==-1)
		{
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
			if(empty(q) && flag1==0)
				flag1=1;
			else if(empty(q) && flag1==1)
				continue;
			else if(!empty(q) && flag1==1)
			{
				printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */				
				pretime[cnt++]=i;
				flag1=0;
				pc=dequeue(&q);
			}
			else pc=dequeue(&q);
		}
/*Decrement burst time to indicate completion of one burst */
		pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
		if(pc.burst==0)
		{
			turnaround=(i+1)-pc.arrival;
			waiting=turnaround-pc.initburst;
			avgta+=turnaround;
			avgwt+=waiting;
			pretime[cnt++]=i+1;
			comp++;
			printf("%s|",pc.name);
			pc.flag=-1;
		}
	}
//Display the times below the Gantt Chart
	printf("\n\t0 ");
	for(i=0;i<cnt;i++)
	{	
		if(pretime[i]>9)
			printf("%d ",pretime[i]);
		else	printf (" %d ",pretime[i]);
	}
//Calculate avg. TA and WT
	avgta=avgta/n;
	avgwt=avgwt/n;
	printf("\n\nAvg. TA time: %.2f",avgta);
	printf("\n\nAvg. WT time: %.2f",avgwt);
}

//Exactly the same as sjf,but with preemption
void sjfpre(struct process p[20],int n)
{
	int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
	float avgta=0,avgwt=0;	
	struct queue q;
	init(&q);
	struct process pc;
	/* pc is the current process. Since initially, there is no process in
	pc, we set its flag to -1*/
	pc.flag=-1;
	printf("\nGANTT CHART\n\n\t|");
	/*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when 	i=1. The loop will run until completed processes(comp) equals no. of proceses
	*/
	for(i=0;comp<n;i++)
	{
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
		for(j=0;j<n;j++)
		{
			if(p[j].arrival<=i && p[j].flag==0)
			{
				p[j].flag=1;
				sjfenqueue(&q,p[j]);
			}
		}
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
		if(pc.flag==-1)
		{
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
			if(empty(q) && flag1==0)
				flag1=1;
			else if(empty(q) && flag1==1)
				continue;
			else if(!empty(q) && flag1==1)
			{
				printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */				
				pretime[cnt++]=i;
				flag1=0;
				pc=dequeue(&q);
			}
			else pc=dequeue(&q);
		}
//The following 'else if' gives the condition for pre-emption. If the process at the front of the queue has smaller burst time than the current process, then current process is enqueued and the new process is dequeued into pc
		else if(q.f!=-1 && pc.burst > q.data[q.f].burst)
		{
			printf("%s|",pc.name);
			pretime[cnt++]=i;
			sjfenqueue(&q,pc);
			pc=dequeue(&q);
		}
/*Decrement burst time to indicate completion of one burst */
		pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
		if(pc.burst==0)
		{
			turnaround=(i+1)-pc.arrival;
			waiting=turnaround-pc.initburst;
			avgta+=turnaround;
			avgwt+=waiting;
			pretime[cnt++]=i+1;
			comp++;
			printf("%s|",pc.name);
			pc.flag=-1;
		}
	}
//Display the times below the Gantt Chart
	printf("\n\t0 ");
	for(i=0;i<cnt;i++)
	{	
		if(pretime[i]>9)
			printf("%d ",pretime[i]);
		else	printf (" %d ",pretime[i]);
	}
//Calculate avg. TA and WT
	avgta=avgta/n;
	avgwt=avgwt/n;
	printf("\n\nAvg. TA time: %.2f",avgta);
	printf("\n\nAvg. WT time: %.2f",avgwt);
}


//Exactly the same as FCFS, except we use prioenqueue function instead of enqueue function
void prio(struct process p[20],int n)
{
	int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
	float avgta=0,avgwt=0;	
	struct queue q;
	init(&q);
	struct process pc;
	/* pc is the current process. Since initially, there is no process in
	pc, we set its flag to -1*/
	pc.flag=-1;
	//Accept the priority for the processes
	for(i=0;i<n;i++)
	{	
		printf("\nEnter priority for process %d: ",i+1);
		scanf("%d",&p[i].prio);
	}	
	printf("\nGANTT CHART\n\n\t|");
	/*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when 	i=1. The loop will run until completed processes(comp) equals no. of proceses
	*/
	for(i=0;comp<n;i++)
	{
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
		for(j=0;j<n;j++)
		{
			if(p[j].arrival<=i && p[j].flag==0)
			{
				p[j].flag=1;
				prioenqueue(&q,p[j]);
			}
		}
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
		if(pc.flag==-1)
		{
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
			if(empty(q) && flag1==0)
				flag1=1;
			else if(empty(q) && flag1==1)
				continue;
			else if(!empty(q) && flag1==1)
			{
				printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */				
				pretime[cnt++]=i;
				flag1=0;
				pc=dequeue(&q);
			}
			else pc=dequeue(&q);
		}
/*Decrement burst time to indicate completion of one burst */
		pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
		if(pc.burst==0)
		{
			turnaround=(i+1)-pc.arrival;
			waiting=turnaround-pc.initburst;
			avgta+=turnaround;
			avgwt+=waiting;
			pretime[cnt++]=i+1;
			comp++;
			printf("%s|",pc.name);
			pc.flag=-1;
		}
	}
//Display the times below the Gantt Chart
	printf("\n\t0 ");
	for(i=0;i<cnt;i++)
	{	
		if(pretime[i]>9)
			printf("%d ",pretime[i]);
		else	printf (" %d ",pretime[i]);
	}
//Calculate avg. TA and WT
	avgta=avgta/n;
	avgwt=avgwt/n;
	printf("\n\nAvg. TA time: %.2f",avgta);
	printf("\n\nAvg. WT time: %.2f",avgwt);
}

//Exactly the same as prio,but with preemption
void priopre(struct process p[20],int n)
{
	int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
	float avgta=0,avgwt=0;	
	struct queue q;
	init(&q);
	struct process pc;
	/* pc is the current process. Since initially, there is no process in
	pc, we set its flag to -1*/
	pc.flag=-1;
	//Accept the priority for the processes
	for(i=0;i<n;i++)
	{	
		printf("\nEnter priority for process %d: ",i+1);
		scanf("%d",&p[i].prio);
	}	
	printf("\nGANTT CHART\n\n\t|");
	/*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when 	i=1. The loop will run until completed processes(comp) equals no. of proceses
	*/
	for(i=0;comp<n;i++)
	{
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
		for(j=0;j<n;j++)
		{
			if(p[j].arrival<=i && p[j].flag==0)
			{
				p[j].flag=1;
				prioenqueue(&q,p[j]);
			}
		}
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
		if(pc.flag==-1)
		{
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
			if(empty(q) && flag1==0)
				flag1=1;
			else if(empty(q) && flag1==1)
				continue;
			else if(!empty(q) && flag1==1)
			{
				printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */				
				pretime[cnt++]=i;
				flag1=0;
				pc=dequeue(&q);
			}
			else pc=dequeue(&q);
		}
	//The following 'else if' gives the condition for pre-emption. If the process at the front of the queue has higher priority than the current process, then current process is enqueued and the new process is dequeued into pc
		else if(q.f!=-1 && pc.prio > q.data[q.f].prio)
		{
			printf("%s|",pc.name);
			pretime[cnt++]=i;
			prioenqueue(&q,pc);
			pc=dequeue(&q);
		}
/*Decrement burst time to indicate completion of one burst */
		pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
		if(pc.burst==0)
		{
			turnaround=(i+1)-pc.arrival;
			waiting=turnaround-pc.initburst;
			avgta+=turnaround;
			avgwt+=waiting;
			pretime[cnt++]=i+1;
			comp++;
			printf("%s|",pc.name);
			pc.flag=-1;
		}
	}
//Display the times below the Gantt Chart
	printf("\n\t0 ");
	for(i=0;i<cnt;i++)
	{	
		if(pretime[i]>9)
			printf("%d ",pretime[i]);
		else	printf (" %d ",pretime[i]);
	}
//Calculate avg. TA and WT
	avgta=avgta/n;
	avgwt=avgwt/n;
	printf("\n\nAvg. TA time: %.2f",avgta);
	printf("\n\nAvg. WT time: %.2f",avgwt);
}

//Procedure for Round Robin
void rr(struct process p[20],int n)
{
	int i,j,qt,tr=0,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
	float avgta=0,avgwt=0;	
	struct queue q;
	init(&q);
	struct process pc;
	/* pc is the current process. Since initially, there is no process in
	pc, we set its flag to -1*/
	pc.flag=-1;
//Obtain quantum time for Round Robin
	printf("\nEnter quantum time: ");
	scanf("%d",&qt);
	printf("\nGANTT CHART\n\n\t|");
	/*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses*/
//Slight modification in the loop for RR. Since the same process will run for qt moments, we increment i by qt instead of by 1	
	for(i=0;comp<n;i++)
	{
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
		for(j=0;j<n;j++)
		{
			if(p[j].arrival<=i && p[j].flag==0)
			{
				p[j].flag=1;
				enqueue(&q,p[j]);
			}
		}
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
		if(pc.flag==-1)
		{
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
			if(empty(q) && flag1==0)
			{
				flag1=1;
				continue;
			}
			else if(empty(q) && flag1==1)
				continue;
			else if(!empty(q) && flag1==1)
			{
				printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */				
				pretime[cnt++]=i;
				flag1=0;
				pc=dequeue(&q);
			}
			else pc=dequeue(&q);
		}
/*If queue is not empty and the current process has already run for qt moments, then we replace the current process by the new process*/
		else if(!empty(q) && tr==qt)
		{
			pretime[cnt++]=i;
			printf("%s|",pc.name);
			enqueue(&q,pc);
			pc=dequeue(&q);
			tr=0;	//Reset time-run.
		}
//Decrement burst time
		pc.burst--;
//tr is time-run. It is required to keep track of whether a process has run for the given quantum time.
		tr++;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
		if(pc.burst==0)
		{
			turnaround=(i+1)-pc.arrival;
			waiting=turnaround-pc.initburst;
			avgta+=turnaround;
			avgwt+=waiting;
			pretime[cnt++]=i+1;
			comp++;
			printf("%s|",pc.name);
			pc.flag=-1;
			tr=0;	//Reset time-run
		}
	}
//Display the times below the Gantt Chart
	printf("\n\t0 ");
	for(i=0;i<cnt;i++)
	{	
		if(pretime[i]>9)
			printf("%d ",pretime[i]);
		else	printf (" %d ",pretime[i]);
	}
//Calculate avg. TA and WT
	avgta=avgta/n;
	avgwt=avgwt/n;
	printf("\n\nAvg. TA time: %.2f",avgta);
	printf("\n\nAvg. WT time: %.2f",avgwt);
}

-----------------------------------------------------------------------------------
    
*/